﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class AssembleStep : BaseRepository_DT<AssembleStepInfo>, IAssembleStep
    {
        #region 静态变量部分
        private const string PARM_ASSEMBLESTEP_PRODUCTSN = "@ProductSN";
        private const string PARM_ASSEMBLESTEP_RECORDTIME = "@RecordTime";
        private const string PARM_ASSEMBLESTEP_USERID = "@UserID";

        private const string PARM_ASSEMBLESTEPITEM_DT = "@AssembleStepItemdt";
        #endregion

        #region 数据检索与写入部分
        public IList<AssembleStepInfo> LoadEntities()
        {
            return null;
        }

        /// <summary>
        /// Get an individual category based on a provided id
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Details about the Category</returns>
        public AssembleStepInfo LoadEntity<T1>(T1 mOID)
        {
            return null;
        }

        public IList<AssembleStepInfo> GetAssembleStepbySN(string productSN)
        {
            SqlParameter parm = GetParameter();
            //Bind the parameter
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("AssembleStep_selectbySN", parm);
        }


        public bool AssembleStepItem_Insert(AssembleStepInfo asi, DataTable itemdt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_ASSEMBLESTEPITEM_DT,itemdt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(asi, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "AssembleStep_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }                
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public IList<AssembleStepInfo> GetAssembleStepbyDT(DateTime starttime, DateTime stoptime)
        {
            SqlParameter[] parms = GetDTParameters();
            parms[0].Value = starttime;
            parms[1].Value = stoptime;
            return ExcuteSqlToGetEntities("AssembleStep_selectbyDT", parms);
        }
        #endregion

        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override AssembleStepInfo GetEntityRecord(SqlDataReader rdr)
        {
            AssembleStepInfo pro = new AssembleStepInfo(rdr.GetValue(0).ToString(),rdr.GetDateTime(1), rdr.GetValue(2).ToString());
            return pro;
        }

        public override AssembleStepInfo GetEntityRecord()
        {
            AssembleStepInfo pro = new AssembleStepInfo();
            return pro;
        }

        public override string GetInsertsp()
        {
            return null;
        }

        public override string GetUpdatesp()
        {
            return null;
        }

        public override string GetDeletesp()
        {
            return null;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_ASSEMBLESTEP_PRODUCTSN, SqlDbType.NVarChar, 50);
            return Parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_ASSEMBLESTEP_PRODUCTSN, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_ASSEMBLESTEP_RECORDTIME, SqlDbType.DateTime, 8),
					new SqlParameter(PARM_ASSEMBLESTEP_USERID, SqlDbType.NVarChar, 10),
                };
            return parms;
        }

        public override void SetParameters(AssembleStepInfo asi, params SqlParameter[] Parms)
        {
            Parms[0].Value = asi.ProductSN;
            Parms[1].Value = asi.RecordTime;
            Parms[2].Value = asi.UserID;
        }
        #endregion
    }
}
